package com.exmaple.mapreducer.salarytotal;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;

//reduce阶段
public class SalaryTotalReducer extends Reducer<IntWritable,IntWritable
        ,IntWritable,IntWritable> {

    private IntWritable v4 = new IntWritable();//工资的总和 k4
    @Override
    protected void reduce(IntWritable key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {
        //1. k4 = k3
        IntWritable k4= key;  //这里的key是reduce阶段的输入的k值
        // 2. v4 = v3元素的和：遍历v3求和
        int total =0;
        for (IntWritable value : values) {
            total+= value.get();
        }
        v4.set(total);
        ///3.输出k4, v4：context.write()方法
        context.write(k4,v4);
    }
}
